# -*- coding: utf-8 -*-
# @author: 龙非池
from __future__ import unicode_literals

from django.db import models

from wlmodels.codes.dicts import cd16_dict, ex04_dict
from wlmodels.codes.fields import CrossForeignCharField
from wlmodels.codes.models import ELBaseModel, EntIdable, ELCA11


# 企业分支机构信息
class ELFiliation(ELBaseModel):
    entobj = models.ForeignKey('company.ELPost', verbose_name='所属企业', db_column='entid', default=0,
                               related_name='c_filiation', to_field='entid')
    bentid = models.ForeignKey('company.ELPost', verbose_name='分支UID', db_column='bentid', default=0,
                               related_name='c_parentent', to_field='entid')
    BRNAME = models.CharField('分支机构名称', max_length=150, null=True, blank=True)
    BRREGNO = models.CharField('分支机构企业注册号', max_length=30, null=True, blank=True)
    BRPRIPID = models.CharField('分支机构中数UID', max_length=36, null=True, blank=True)
    BRPRINCIPAL = models.CharField('分支机构负责人', max_length=30, null=True, blank=True)
    CBUITEM = models.TextField('一般经营项目', null=True, blank=True)
    BRADDR = models.CharField('分支机构地址', max_length=100, null=True, blank=True)
    updated = models.DateTimeField('更新时间', null=True, blank=True)

    class Meta:
        verbose_name = '企业分支机构'
        verbose_name_plural = '企业分支机构'
        app_label = "company"
        db_table = 'company_filiation'

    def _entid(self):
        from wlmodels.company.models import ELPost
        entid = ELPost.objects.filter(entid=self.entobj_id).first()
        if entid:
            return entid
        return ""

    _entid.short_description = "所属企业"

    entid = property(_entid)

    def __unicode__(self):
        return u'%s的分支机构' % self.entid


# 集团信息
class ELGroup(EntIdable):
    pentid = models.IntegerField("母公司UID", null=True, blank=True)
    GRPNAME = models.CharField('集团名称', max_length=200, null=True, blank=True, db_index=True)
    GRPSHFORM = models.TextField('集团简称', null=True, blank=True)
    GRPREGCNO = models.CharField('集团登记证号', max_length=30, null=True, blank=True)
    PCOMNAME = models.CharField('母公司名称', max_length=200, null=True, blank=True)
    PCOMREGNO = models.CharField('母公司注册号', max_length=30, null=True, blank=True)
    PCOMANDSCOMCAPSUM = models.DecimalField('母公司和子公司注册资本总和', max_digits=15, decimal_places=2, null=True, blank=True, help_text='万元')
    ESDATE = models.DateField('成立日期', null=True, blank=True)
    APPEDATE = models.DateField('核准日期', null=True, blank=True)
    REGORG_id = CrossForeignCharField('登记机关', db_column='REGORG', max_length=8, null=True, blank=True)
    updated = models.DateTimeField('更新时间', null=True, blank=True)

    class Meta:
        verbose_name = '集团信息'
        verbose_name_plural = '集团信息'
        app_label = "companyplus"
        db_table = 'company_group'

    def __unicode__(self):
        return u'%s的集团信息' % self.entid

    def _regorg(self):
        the_regorg = list(ELCA11.objects.filter(code=self.REGORG_id).values_list("name", flat=True))
        if len(the_regorg):
            return the_regorg[0]
        return ""

    _regorg.short_description = "登记机关"

    REGORG = property(_regorg)


# 集团注销信息
class ELGroupCancel(EntIdable):
    CANDOCNO = models.CharField('注销文号', max_length=50, null=True, blank=True)
    CANREA = models.CharField('注销原因', max_length=3, null=True, blank=True, choices=cd16_dict)
    CANDATE = models.DateField('注销日期', null=True, blank=True)
    updated = models.DateTimeField('更新时间', null=True, blank=True)

    class Meta:
        verbose_name = '集团注销信息'
        verbose_name_plural = '集团注销信息'
        app_label = "companyplus"
        db_table = 'company_group_cancel'

    def __unicode__(self):
        return u'%s的集团注销信息' % self.entid


# 集团成员信息
class ELGroupMember(EntIdable):
    MEMNAME = models.CharField('集团成员名称', max_length=200, null=True, blank=True)
    REGNO = models.CharField('集团成员注册号', max_length=30, null=True, blank=True)
    MEM_ID = models.CharField('成员ID', max_length=50, null=True, blank=True)
    GRPMEMTYPE = models.CharField('集团成员类型', max_length=2, null=True, blank=True, choices=ex04_dict)
    REGCAP = models.DecimalField('集团成员注册资本', max_digits=15, decimal_places=2, null=True, blank=True, help_text='万元')
    updated = models.DateTimeField('更新时间', null=True, blank=True)

    class Meta:
        verbose_name = '集团成员信息'
        verbose_name_plural = '集团成员信息'
        app_label = "companyplus"
        db_table = 'company_group_member'

    def __unicode__(self):
        return u'%s的集团成员信息' % self.entid


models_relation_imported = 0  # 仅供主model引用的虚变量
